#!/bin/sh
#vim:set expandtab sts=4 ts=4 sw=4 tw=80:
# This script helps to automate the process of regenerating the static
# freedict.org web site with the latest dynamic content provided by its plugins.
# It assumes that it is run on the FreeDict server. There should be a base path
# for the API files and another for the HTML files. The reason to keep it
# separate is that lektor clean removes all files in the www directory. This
# script copies files from to the $API_BASE.
# WARNING: this script drops changes in i18n/contents+<langcode>.po, because
# these are usually only date changes. If you happen to have changes there,
# commit them first!
set -e
# where freedict-database.* is found
API_BASE=/var/www
WWW_BASE=/var/www/html

if ! [ -f $API_BASE/freedict-database.json ]; then
	  echo "Error, freedict-database.json not found, please make sure that it's in $API_BASE	or adjust the path in this script."
	  exit 1
fi

export LC_ALL=en_US.UTF-8
export PATH="$HOME/.local/bin:$PATH"

# change to directory where this script dwells
SCRIPT_PATH=$(readlink -f "$0")
cd $(dirname "$SCRIPT_PATH")

if [ ! -f freedict-database.json ]; then
	  ln -s -f $API_BASE/freedict-database.json
fi
# drop changes, it's only the latest generation date which changed
git checkout i18n/contents+*.po 2>&1 | (grep -v -E -i 'updated.*path' || true)
git pull -r > /dev/null 2>&1
lektor clean --yes --output-path $WWW_BASE.tmp > /dev/null || true

LEKTOR_LOG=/tmp/lektor_build.log
lektor build --output-path $WWW_BASE.tmp > $LEKTOR_LOG 2>&1
RET=$?
if [ $RET -ne 0 ] || grep -E '^[[:space:]]+E ' < $LEKTOR_LOG > /dev/null; then
    cat $LEKTOR_LOG
    exit $RET
fi


# copy API files
cd $WWW_BASE.tmp
cp $API_BASE/freedict-database.json .
cp $API_BASE/freedict-database.xml .
cd ..
rm -rf $WWW_BASE
mv $WWW_BASE.tmp $WWW_BASE
exit 0
